﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace QrCodeUtils.Utils
{
    class CommenUtils
    {
        /// <summary>
        /// 根据bitmap的长宽来大概定位dpi是多少，针对A4纸打印尺寸
        /// 101dpi：100+ x 900
        /// 150dpi：1000x1800
        /// 200dpi：1488x2105
        /// 300dpi：2477x3491
        /// 400dpi：3287x4767
        /// 600dpi以上：4000+ x 5000+
        /// </summary>
        /// <param name="bitmap">原图</param>
        /// <returns></returns>
        public static int DeCodeDpiFromBitmap(Bitmap bitmap)
        {
            int DpiTag = 0;//1000
            int width = bitmap.Width;
            int height = bitmap.Height;
            int temp = Math.Max(width, height);
            if (1000 >= temp)
                DpiTag = 101;
            if (2000 >= temp && temp > 1000)
                DpiTag = 150;
            if (3000 >= temp && temp > 2000)
                DpiTag = 200;
            if (4000 >= temp && temp > 3000)
                DpiTag = 300;
            if (5000 >= temp && temp > 4000)
                DpiTag = 400;
            if (5000 < temp)
                DpiTag = 601;
            return DpiTag;
        }
        /// <summary>
        /// 处理图片灰度
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static Bitmap MakeGrayscale3(Bitmap original)
        {
            #region
            //create a blank bitmap the same size as original
            Bitmap newBitmap;
            try
            {
                double Width = original.Width * 1;
                double Height = original.Height * 1;
                int WidthInt = (int)Width;
                int HeightInt = (int)Height;
                newBitmap = new Bitmap(WidthInt, HeightInt);
                //get a graphics object from the new image
                Graphics g = Graphics.FromImage(newBitmap);

                //create the grayscale ColorMatrix
                System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
                   new float[][]
                   {
                        new float[] {.3f, .3f, .3f, 0, 0},
                        new float[] {.59f, .59f, .59f, 0, 0},
                        new float[] {.11f, .11f, .11f, 0, 0},
                        new float[] {0, 0, 0, 1, 0},
                        new float[] {0, 0, 0, 0, 1}
                   });

                //create some image attributes
                ImageAttributes attributes = new ImageAttributes();

                //set the color matrix attribute
                attributes.SetColorMatrix(colorMatrix);

                //draw the original image on the new image
                //using the grayscale color matrix
                g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
                   0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
                CommenUtils.ClearMemory();
                //dispose the Graphics object
                g.Dispose();
            }
            catch (Exception ex)
            {
                CommenUtils.ClearMemory();
                return original;
            }
            return newBitmap;
            #endregion
        }

        #region 释放内存
        [System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        /// <summary>
        /// 释放内存
        /// </summary>
        public static void ClearMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
        #endregion 

    }
}
